package com.libing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.libing.base.BaseDao;
import com.libing.base.BaseService;
import com.libing.dao.CustomerDao;
import com.libing.entity.Customer;
import com.libing.entity.CustomerAllInfo;
import com.libing.entity.CustomerLevel;
import com.libing.entity.User;
import com.libing.enums.StateEnum;
import com.libing.service.CustomerService;
import com.libing.util.StringUtil;
import com.libing.web.vo.CustomerVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 客户信息 CRUD Service
 * @Auther: LEE
 * @Date: Create in 2018/2/4 11:45
 */
@Service
public class CustomerServiceImpl extends BaseService<Customer> implements CustomerService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private TranService tranService;

    @Override
    public StateEnum insert(Customer customer) {
        return getCUDStatus(getBaseDao().insert(customer));
    }

    @Override
    public StateEnum update(Customer customer) {
        return getCUDStatus(getBaseDao().update(customer));
    }

    @Override
    public StateEnum delete(Long id) {
        return getCUDStatus(getBaseDao().delete(id));
    }

    @Override
    public Customer selectById(Long id) {
        try {
            return getBaseDao().selectById(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public Boolean insert(CustomerAllInfo customerAllInfo, User user) {
        Customer customer = tranService.splitInfo(customerAllInfo, user);
        if (customer != null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public PageInfo<CustomerVO> queryCustomersByParams(Integer pageNum, String nameOrContact, String companyName, Long levelId) {
        try {

            String name = null;
            String contact = null;
            if (nameOrContact != null && nameOrContact != "") {
                if (StringUtil.isNumeric(nameOrContact)) {
                    contact = nameOrContact;
                } else {
                    name = nameOrContact;
                }
            }

            Integer minScore = null;
            Integer maxScore = null;
            List<CustomerLevel> customerLevel = null;
            if (levelId != null && levelId != 0) {
                customerLevel = customerDao.queryCustomerLevel(levelId);
                minScore = customerLevel.get(0).getMinScore();
                maxScore = customerLevel.get(0).getMaxScore();
            }
            PageHelper.startPage(pageNum, 10);
            List<CustomerVO> customerVOS = customerDao.findCustomersByParams(name, contact, companyName, minScore, maxScore);

            if (levelId == null) {
                List<CustomerLevel> customerLevelList = customerDao.queryCustomerLevel(null);
                for (int i = 0; i < customerVOS.size(); i++) {
                    for (int j = 0; j < customerLevelList.size(); j++) {
                        Integer min_score = customerLevelList.get(j).getMinScore();
                        Integer max_score = customerLevelList.get(j).getMaxScore();
                        if (customerVOS.get(i).getScore() >= min_score && customerVOS.get(i).getScore() < max_score) {
                            customerVOS.get(i).setLevel(customerLevelList.get(j).getName());
                            break;
                        }
                    }
                }
            } else {
                for (int i = 0; i < customerVOS.size(); i++) {
                    customerVOS.get(i).setLevel(customerLevel.get(0).getName());
                }
            }
            PageInfo<CustomerVO> pageInfo = new PageInfo<CustomerVO>(customerVOS);
            return pageInfo;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public List<CustomerLevel> queryCustomerLevel(Long id) {
        return customerDao.queryCustomerLevel(id);
    }

    @Override
    public StateEnum insertCusLevel(CustomerLevel customerLevel) {
        return getCUDStatus(customerDao.insertCusLevel(customerLevel));
    }

    @Override
    public StateEnum updateCusLevel(CustomerLevel customerLevel) {
        if (customerLevel.getId() == null && ("").equals(customerLevel.getId())) {
            return StateEnum.FAIL;
        }
        return getCUDStatus(customerDao.updateCusLevel(customerLevel));
    }

    @Override
    public StateEnum deleteCusLevel(Integer id) {
        return getCUDStatus(customerDao.deleteCusLevel(id));
    }

    @Override
    protected BaseDao<Customer> getBaseDao() {
        return this.customerDao;
    }

}
